/**
 * @author dell
 */
/**
 * @author dell
 *
 */
package com.p2p.port.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.p2p.common.constants.Message;
import com.p2p.common.enums.ResponseEnum;
import com.p2p.common.utils.Underline2Camel;
import com.p2p.port.po.MonitorPort;
import com.p2p.port.service.PortService;
import com.p2p.wallet.po.Wallet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "port")
public class PortController {

    private static final Logger logger = LoggerFactory.getLogger(PortController.class);
    @Autowired
    private PortService portService;


    @RequestMapping(value = "selectPortOption", method = RequestMethod.GET)
    public Message selectPortOption(HttpServletRequest request) {

        List<MonitorPort> portList = portService.selectList(new EntityWrapper<MonitorPort>().eq("status", 1));
        return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), portList);
    }


    @RequestMapping(value = "insertPort", method = RequestMethod.POST)
    public Message insertPort(HttpServletRequest request, MonitorPort port) {
        System.out.println("进入");
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        port.setStatus(1);
        port.setCreateTime(timestamp);
        portService.insert(port);
        return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    @RequestMapping(value = "updatePort", method = RequestMethod.POST)
    public Message updatePort(HttpServletRequest request, MonitorPort port) {
        System.out.println("编号为" + port.getRunTime());
        portService.updateById(port);
        return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    /**
     * 逻辑删除用户
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "deletePort", method = RequestMethod.GET)
    public Message deletePort(HttpServletRequest request) {
        Long Id = Long.parseLong(request.getParameter("walletid"));
        MonitorPort port = portService.selectById(Id);
        port.setStatus(-1);
        portService.updateById(port);
        return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    /**
     * 获取用户 By UserId
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "selectPortById", method = RequestMethod.GET)
    public Message selectPortById(HttpServletRequest request) {
        Long id = Long.parseLong(request.getParameter("id"));
        MonitorPort port = portService.selectById(id);
        return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), port);
    }

    @RequestMapping(value = "selectPort", method = RequestMethod.GET)
    public Map selectPort(HttpServletRequest request, Wallet wallet) {
        System.out.println("调用了查询接口");
        EntityWrapper<MonitorPort> userWrapper = new EntityWrapper<MonitorPort>();
        userWrapper.eq("status", 1);
        String sortField = Underline2Camel.camel2Underline(request.getParameter("sort") == null ? "id" : request.getParameter("sort"), true);
        String orderField = request.getParameter("order") == null ? "desc" : request.getParameter("order");

        List<MonitorPort> userList = portService.selectList(sortField, orderField);
        Map result = new HashMap();
        System.out.println(userList.size());
        result.put("rel", true);
        result.put("msg", "SUCCESS");
        result.put("list", userList);
        result.put("count", userList.size());

        return result;
    }

}